//
//  RSRetweetView.swift
//  RSWeibo
//
//  Created by Macx on 16/8/14.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit
import SnapKit

// 控件跟边界的差距, 以及部分控件之间距离
let retWeetViewMargin: CGFloat = 8



class RSRetweetView: UIView {
    
    //  记录转发微博视图的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    // 拥有一个转发微博模型
    var retweetViewModel: RSStatusViewModel? {
    
        didSet{
        
            textLab.text = retweetViewModel?.retWeetContent
   
            
            // 给 配图视图 添加数据源方法
            picturesView.picUrls = retweetViewModel?.statusModel?.retweeted_status?.pic_urls
            
//              卸载上一次约束
            retweetViewBottomConstraint?.uninstall()
            
//            print("总共的转发配图\(retweetViewModel?.statusModel?.retweeted_status?.pic_urls)")
            
            //  判断配图是否存在并且是否大于0
            if let picUrls = retweetViewModel?.statusModel?.retweeted_status?.pic_urls where picUrls.count > 0 {
                //  显示配图
                picturesView.hidden = false
                //  设置配图信息
                picturesView.picUrls = picUrls
                //  更新约束
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint =  make.bottom.equalTo(picturesView).offset(retWeetViewMargin).constraint
                })
                
                
            } else {
                //  隐藏配图
                picturesView.hidden = true
                
//                self.snp_remakeConstraints { (make) in
//                    make.bottom.equalTo(textLab).offset(retWeetViewMargin)
//                    make.left.equalTo(self)
//                    make.right.equalTo(self)
//                    make.top.equalTo(self)
//                }
                //  更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(textLab).offset(retWeetViewMargin).constraint
                })
            }

        }
    }

    // 拥有一个正文label
    private lazy var textLab = UILabel(fontSize: 15, textColor: UIColor.blackColor())
    
    // 配图视图
    private lazy var picturesView: RSPictureView = RSPictureView()
    
    // 设置约束
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 视图背景是灰色的

        backgroundColor = UIColor(white: 239/255.0, alpha: 0.8)
        
        textLab.numberOfLines = 0
        
         setupRetWeetView()
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置界面
    private func setupRetWeetView(){
    
        // 添加到父控件中
        addSubview(textLab)
        
        // 把 配图控件添加到父控件中
        addSubview(picturesView)
        
        // 添加约束
        textLab.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(retWeetViewMargin)
            make.leading.equalTo(self).offset(retWeetViewMargin)
            make.trailing.equalTo(self).offset(retWeetViewMargin)
            
        }
        
        picturesView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(textLab)
            make.top.equalTo(textLab.snp_bottom).offset(retWeetViewMargin)
            make.size.equalTo(CGSizeMake(100, 100))
            
        }
        
        // 必须要给 本视图self 添加具体的约束, 否则在cell中设置约束时 因为无法计算RSRetweetView的高度而导致布局混乱
        
//        self.snp_makeConstraints { (make) in
//            make.bottom.equalTo(textLab).offset(retWeetViewMargin)
//            make.left.equalTo(self)
//            make.right.equalTo(self)
//            make.top.equalTo(self)
//        }
        
        self.snp_makeConstraints { (make) in
          self.retweetViewBottomConstraint = make.bottom.equalTo(picturesView).offset(retWeetViewMargin).constraint
        }
        
    }
    
    

}
